package ivf

import (
	"math"
)

// 计算余弦相似度
func cosineSimilarity(vec1, vec2 []float32) float32 {
	var dotProduct, normA, normB float32
	for i := range vec1 {
		dotProduct += vec1[i] * vec2[i]
		normA += vec1[i] * vec1[i]
		normB += vec2[i] * vec2[i]
	}
	if normA == 0 || normB == 0 {
		return 0
	}
	return dotProduct / (float32(math.Sqrt(float64(normA))) * float32(math.Sqrt(float64(normB))))
}

// 结构体用于存储文档和相似度
type DocSimilarity struct {
	Doc BOW
	Sim float32
}

// 实现 sort.Interface 接口
type BySimilarity []DocSimilarity

func (a BySimilarity) Len() int           { return len(a) }
func (a BySimilarity) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a BySimilarity) Less(i, j int) bool { return a[i].Sim > a[j].Sim } // 降序

type TextProof struct {
	VO        map[int]Volist
	ResultBOW []BOW
}
